home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13269 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: linux.nildram.co.uk!news
  2. From: colin@greench.co.uk (Colin Wray)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Why does my program do this?
  5. Date: Fri, 05 Apr 1996 18:36:15 GMT
  6. Organization: Greenchurch Software Ltd
  7. Message-ID: <4k3p49$c0t@linux.nildram.co.uk>
  8. References: <4jnln2$95j@dfw-ixnews3.ix.netcom.com> <4jpe4s$db2@penage.cs.laurentian.ca>
  9. NNTP-Posting-Host: pppj.nildram.co.uk
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. echan@nickel.laurentian.ca (Edward C. Chan) wrote:
  13.  
  14. >>char scores[STUDENT][5];
  15. >>
  16. >>    for (i=0; i <= students-1; i=i+1)
  17. >>        {
  18. >>            for (j=0; j <= tests-1; j=j+1)
  19. >>            {
  20. >>                gets(&scores[i][j]);
  21. >>            }
  22. >>        }
  23. >>
  24.  
  25. >user error.
  26.  
  27. >replace 
  28.  
  29. >   gets(&scores[i][j]);
  30.  
  31. >with
  32.  
  33. >   gets(scores[i][j]);
  34.  
  35. >andeverything shall work fine.  Actually, you are lucky that the programme
  36. >doesn't crash on you.
  37.  
  38. >Hope that helps.
  39.  
  40. >Ed.
  41.  
  42. What a load of rubbish ! scores[i][j] is a single char whose address
  43. is scores[i] + j;
  44. The problem is that no space has been allocated for the string from
  45. gets(); He needs 5 strings for each student, so the solution is to
  46. declare scores[STUDENTS][TESTS][16]; (say). Then gets(scores[i][j]) is
  47. ok. Of course, an array of structs would be better.
  48. colin@greench.co.uk
  49.  
  50.